﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SSCAI.Common;
using SSCAI.Model;
namespace SSCAI.Client
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        SnModels sn = new SnModels();

        public Login()
        {
            InitializeComponent();

            if (sn.Sns.Count > 0)
            {
                SSCAI.Common.CalculateHelper.ExpiredDate = Convert.ToDateTime(sn.Sns[0].ExpiredDate);
                SSCAI.Common.CalculateHelper.UserName = sn.Sns[0].UserName;
                this.txtCode.Text = sn.Sns[0].Sn;
            }
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            var snCode = this.txtCode.Text.Trim();
            var expriedDate = SSCAI.Common.CalculateHelper.GetExpriedDate(snCode);
            var username = SSCAI.Common.CalculateHelper.GetUserName(snCode);

            if (DateTime.Now <= expriedDate)
            {
                if (this.txtUserName.Text.Trim().Equals(username))
                {
                    if (sn.Sns.Count == 0 || snCode != sn.Sns[0].Sn)
                    {
                        sn.UpdateUserInfo(snCode);

                    }

                    MainWindow main = new MainWindow();
                    main.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("用户名不正确！");
                    return;
                }

            }
            else
            {
                MessageBox.Show("软件过期！");
                return;
            }

            //SnWindow window = new SnWindow();
            //window.Show();
        }

        private void btnCanel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
